package com.example.liuhb.NOME;

import android.app.ProgressDialog;
import android.content.Context;
import android.widget.Toast;

import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 根据道铂的接口做封装，只返回data里面的数据，最终返回的是经过Gson解析的JOPO
 * Created by Administrator on 2018/3/10.
 */

public abstract class ResultCallOrder<T> {
    Context context;

    public ResultCallOrder(Context context, Call<T> call) {
        this.context = context;
        final ProgressDialog dialog = ProgressDialog.show(context, "提示", "请稍等...");
        //请求并回调
        call.enqueue(new Callback<T>() {
            @Override
            public void onResponse(Call<T> call, Response<T> response) {
                if (dialog !=null){
                    dialog.dismiss();
                }

                if (null == response) {
                    //未知异常情况
                    System.out.println("-------900, false,response null---------------------");
                    ResultCallOrder.this.onFailure(-900, false, "response null");
                    return;
                }

                int httpCode = response.code();
                System.out.println("httpCode:"+httpCode);
                if (!response.isSuccessful()) {
                    ResponseBody errorBody = response.errorBody();

                    if (null != errorBody) {
                        try {
                            Gson error = new Gson();
                            System.out.println("------errorBody---------------------");
                            GetOrdersResult r = error.fromJson(errorBody.string(), GetOrdersResult.class);
                            if (null == r) {
                                ResultCallOrder.this.onFailure(httpCode, false, "null");
                            } else {
                                if(r.error!=null){
                                    ResultCallOrder.this.onFailure(httpCode,false,r.error.message);
                                }

                            }
                        }  catch (Exception e) {
                            System.out.println("------Exception---------------------");
                            ResultCallOrder.this.onFailure(httpCode, false, e.getMessage());
                        }
                    } else {
                        System.out.println("------errorBody null---------------------");
                        ResultCallOrder.this.onFailure(httpCode, false, "errorBody null");
                    }
                    return;
                }
                T result = response.body();
                System.out.println("--------- response.body() +result:"+result);
                if (null == result) {
                    //未知异常情况
                    System.out.println("------response.body null---------------------");
                    ResultCallOrder.this.onFailure(httpCode, false, "response.body null");
                    return;
                }
            }

            @Override
            public void onFailure(Call<T> call, Throwable t) {
                if (dialog !=null){
                    dialog.dismiss();
                }
                ResultCallOrder.this.onFailure(-901, false, t.getMessage());
            }
        });
    }



    public abstract void onSuccess(GetOrdersResult obj);

    protected void onFailure(int httpCode, boolean IsOK, String msg) {
        toast(httpCode + "; " + IsOK + "; " + msg);
    }

    public void toast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
    }

    public void toast(int resID) {
        toast(context.getResources().getString(resID));
    }
}
